package com.shycloud.mido.basic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.basic.api.dto.DictionaryDTO;
import com.shycloud.mido.basic.api.entity.BasDictionary;
import com.shycloud.mido.basic.api.enums.MessageCodeEnum;
import com.shycloud.mido.basic.api.vo.DictionaryVO;
import com.shycloud.mido.basic.mapper.BasDictionaryMapper;
import com.shycloud.mido.basic.service.BasDictionaryService;
import com.shycloud.mido.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-02-26
 */
@Service
@AllArgsConstructor
public class BasDictionaryServiceImpl extends ServiceImpl<BasDictionaryMapper, BasDictionary> implements BasDictionaryService {

	private final BasDictionaryMapper basDictionaryMapper;

	/**
	 * 字典表查询
	 *
	 * @param dto
	 * @return
	 */
	@Override
	public List<DictionaryVO> sel(DictionaryDTO dto) {
		List<DictionaryVO> listVo = new ArrayList<DictionaryVO>();
		List<BasDictionary> basDictionaryList = basDictionaryMapper.sel(dto);
		for (int i = 0; i < basDictionaryList.size(); i++) {
			DictionaryVO vo = new DictionaryVO();
			try {
				BeanUtils.copyProperties(basDictionaryList.get(i), vo);
			} catch (BeansException e) {
				e.printStackTrace();
			}
			listVo.add(vo);
		}
		return listVo;
	}

	/**
	 * 查询字典label
	 *
	 * @param type  字典类别
	 * @param value 数据值
	 * @return
	 */
	@Override
	public String getDic(String type, String value) {
		return basDictionaryMapper.getDic(type, value);
	}

	/**
	 * 课程详情-联系情况字典表查询
	 *
	 * @return
	 */
	@Override
	public R practiceDic() {
		Map<String, Object> map = new HashMap<>();
		//练习范围
		List<BasDictionary> scopeList = basDictionaryMapper.practiceDic("scope_practice");
		//练习进度
		List<BasDictionary> practiceSpeedList = basDictionaryMapper.practiceDic("practice_speed");
		//音准
		List<BasDictionary> intonationList = basDictionaryMapper.practiceDic("intonation");
		//弹奏技巧
		List<BasDictionary> skillsList = basDictionaryMapper.practiceDic("playing_skills");
		//熟练度
		List<BasDictionary> proficiencyList = basDictionaryMapper.practiceDic("proficiency");
		//节奏感
		List<BasDictionary> rhythmList = basDictionaryMapper.practiceDic("rhythm");
		//速度
		List<BasDictionary> speedList = basDictionaryMapper.practiceDic("speed");
		map.put("scopePractice", scopeList);
		map.put("practiceSpeed", practiceSpeedList);
		map.put("intonation", intonationList);
		map.put("playingSkills", skillsList);
		map.put("proficiency", proficiencyList);
		map.put("rhythm", rhythmList);
		map.put("speed", speedList);
		return new R(map, MessageCodeEnum.SUCCESS.getMessage());
	}

	/**
	 * 学生评价陪练教师标签字典表查询
	 *
	 * @return
	 */
	@Override
	public R tagDic() {
		List<BasDictionary> list = basDictionaryMapper.practiceDic("tag");
		return new R(list, MessageCodeEnum.SUCCESS.getMessage());
	}

	/**
	 * 上课偏好标签字典查询
	 *
	 * @return
	 */
	@Override
	public R preferenceDic() {
		Map<String, Object> map = new HashMap<>();
		//上课偏好1
		List<BasDictionary> preference1List = basDictionaryMapper.practiceDic("preference1");
		//上课偏好2
		List<BasDictionary> preference2List = basDictionaryMapper.practiceDic("preference2");
		//上课偏好3
		List<BasDictionary> preference3List = basDictionaryMapper.practiceDic("preference3");
		//上课偏好4
		List<BasDictionary> preference4List = basDictionaryMapper.practiceDic("preference4");
		map.put("preference1", preference1List);
		map.put("preference2", preference2List);
		map.put("preference3", preference3List);
		map.put("preference4", preference4List);
		return new R(map, MessageCodeEnum.SUCCESS.getMessage());
	}


	/**
	 * 重写字典表查询
	 *
	 * @param type 类别
	 * @return
	 */
	@Override
	public Map<String, String> dicMap(String type) {
		Map<String, String> map = new HashMap<>();
		DictionaryDTO dictionaryDTO = new DictionaryDTO();
		dictionaryDTO.setType(type);
		List<BasDictionary> list = basDictionaryMapper.sel(dictionaryDTO);
		for (int i = 0; i < list.size(); i++) {
			map.put(list.get(i).getValue(), list.get(i).getLabel());
		}
		return map;
	}

	@Override
	public Map<String, Map<String, String>> getDicMap(List<String> types) {
		Map<String, Map<String, String>> resMap = new HashMap<>();
		for (String type : types) {
			resMap.put(type, dicMap(type));
		}
		return resMap;
	}

}
